package com.simple.accounting.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.simple.accounting.R;
import com.simple.accounting.db.UserDBManager;
import com.simple.accounting.db.UserInfoBean;
import com.simple.accounting.utils.ToastUtils;


/**
 * @Author: huwenfei
 * 注册模块
*/
public class RegistryActivity extends BaseActivity {

    private Button registry;
    private EditText edt_name;
    private EditText edt_pwd;
    private EditText edt_pwd_again;
    private TextView mTextView;
    private TextView mTextView1;
    private ImageView show1;
    private ImageView show2;
    private ImageView back;
    private UserInfoBean mUserInfoBean = new UserInfoBean();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activty_registry);
        registry = findViewById(R.id.registryBtn);
        edt_name = findViewById(R.id.edtName);
        edt_pwd = findViewById(R.id.edtPwd);
        mTextView = findViewById(R.id.tv_tip);
        mTextView1 = findViewById(R.id.tv_tip1);
        edt_pwd_again = findViewById(R.id.edt_pwd_again);
        show1 = findViewById(R.id.show1);
        show2 = findViewById(R.id.show2);
        back = findViewById(R.id.registry_iv_back);
        edtNameCheckLength();
        edtPwdCheckLength();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        registry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    addToDB();
            }
        });

        show1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleShow1();
            }
        });
        show2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleShow2();
            }
        });
    }

    boolean isShow1 = true;
    boolean isShow2 = true;

    /**
     * 点击头布局眼睛时，如果原来是明文，就加密，如果是密文，就显示出来
     */
    private void toggleShow1() {
        if (isShow1) {   //明文====》密文
            edt_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            int len = edt_pwd.getText().toString().trim().length();
            edt_pwd.setSelection(len);
            show1.setImageResource(R.mipmap.hide);
            isShow1 = false;   //设置标志位为隐藏状态
        } else {  //密文---》明文
            edt_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            int len = edt_pwd.getText().toString().trim().length();
            edt_pwd.setSelection(len);
            show1.setImageResource(R.mipmap.show);
            isShow1 = true;   //设置标志位为隐藏状态
        }
    }

    private void toggleShow2() {
        if (isShow2) {   //明文====》密文
            edt_pwd_again.setTransformationMethod(PasswordTransformationMethod.getInstance());
            int len = edt_pwd_again.getText().toString().trim().length();
            edt_pwd_again.setSelection(len);
            show2.setImageResource(R.mipmap.hide);
            isShow2 = false;   //设置标志位为隐藏状态
        } else {  //密文---》明文
            edt_pwd_again.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            int len = edt_pwd_again.getText().toString().trim().length();
            edt_pwd_again.setSelection(len);
            show2.setImageResource(R.mipmap.show);
            isShow2 = true;   //设置标志位为隐藏状态
        }
    }

    public void edtNameCheckLength(){
        edt_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                Log.i("TAG", "onFocusChange: "+edt_name.getText().toString()+edt_name.hasFocus());
                if (!edt_name.hasFocus()){
                    if (edt_name.getText().toString().length()>9||edt_name.getText().toString().length()<3){
                        //这行代码解决如果mTextView起初设置为了INVISIBLE的情况下，要让mTextView能显示出来
                        mTextView.setVisibility(View.VISIBLE);
                        mTextView.setText("用户名长度仅支持在3-9位！");
                        mTextView.setTextColor(Color.RED);
                        registry.setClickable(false);
                    }else {
                        mTextView.setVisibility(View.INVISIBLE);
                        registry.setClickable(true);//将注册按钮设置可点击
                    }
                }
            }
        });
    }

    public void edtPwdCheckLength(){
        edt_pwd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!edt_pwd.hasFocus()){
                    if (edt_pwd.getText().toString().length()>12||edt_pwd.getText().toString().length()<6){
                        mTextView1.setVisibility(View.VISIBLE);
                        mTextView1.setText("密码长度仅支持6-12位");
                        mTextView1.setTextColor(Color.RED);
                        registry.setClickable(false);
                    }else {
                        mTextView1.setVisibility(View.INVISIBLE);
                        registry.setClickable(true);
                    }
                }
            }
        });
    }
    public void addToDB(){
        String name = edt_name.getText().toString().trim();
        String pwd = edt_pwd.getText().toString().trim();
        String pwd_again = edt_pwd_again.getText().toString().trim();
        Log.i("TAG", "addToDB: "+name);

        if (TextUtils.isEmpty(name)){
            ToastUtils.show(this,"用户名不能为空！");
            return;
        }
        if (TextUtils.isEmpty(pwd)){
            ToastUtils.show(this,"密码不能为空！");
            return;
        }
        if (TextUtils.isEmpty(pwd_again)){
            ToastUtils.show(this,"确认密码不能为空！",ToastUtils.TYPE_ERROR);
            return;
        }
        if (pwd.equals(pwd_again)){
            mUserInfoBean.setUsername(name);
            mUserInfoBean.setPassword(pwd);
            long userInfo = UserDBManager.insertItemToUserTB(mUserInfoBean);
            Log.i("TAG", "addToDB: "+userInfo);
                if (userInfo!=-1){
                    ToastUtils.show(this,"注册成功!",ToastUtils.TYPE_SUCCESS);
                    Intent intent = new Intent(RegistryActivity.this,LoginActivity.class);
                    intent.putExtra("name",name);
                    intent.putExtra("pwd",pwd);
                    startActivity(intent);
                }else {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(RegistryActivity.this)
                            .setTitle("提醒")
                            .setMessage("该用户已经存在，换个用户名再注册吧！")
                            .setCancelable(false)
                            .setPositiveButton("确定",null);
                    dialog.show();
                }

        }else {
            AlertDialog.Builder dialog = new AlertDialog.Builder(RegistryActivity.this)
                    .setTitle("提醒")
                    .setMessage("两次输入的密码不相同")
                    .setCancelable(false)
                    .setPositiveButton("确定",null);
            dialog.show();
        }
    }


}
